using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MettleSystems.dashCommerce.Store;
using SubSonic.Utilities;

namespace MettleSystems.dashCommerce.Web {
  public partial class search : MettleSystems.dashCommerce.Controls.BasePage {

    #region Member Variables

    private PagedDataSource pagedDataSource = new PagedDataSource();
    private string searchTerms = string.Empty;

    #endregion

    #region Page Events

    protected void Page_Load(object sender, EventArgs e) {
      searchTerms = HttpUtility.UrlDecode(Utility.GetParameter("searchTerms"));
      string p = Utility.GetParameter("p");

      if (!string.IsNullOrEmpty(searchTerms)) {
        if (string.IsNullOrEmpty(p)) {
          if (Master.SiteSettings.CollectSearchTerms) {
            BrowsingLogController.LogBrowsingInfo(searchTerms, BrowsingBehaviour.Search, Request.Url.ToString().PadRight(255).Substring(0, 254).Trim(), WebUtility.SessionId(), WebUtility.GetUserName());
          }
        }
        LoadProducts();
      }
      //Page.Title = string.Format(WebUtility.MainTitleTemplate, Master.SiteSettings.SiteName, searchTerms);
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Loads the products.
    /// </summary>
    private void LoadProducts() {
      pagedDataSource.AllowPaging = true;
      pagedDataSource.PageSize = Master.SiteSettings.CatalogItems;
      int currentPageIndex = 0;
      int.TryParse(Utility.GetParameter("p"), out currentPageIndex);
      pagedDataSource.CurrentPageIndex = currentPageIndex;

      pagedDataSource.DataSource = new ProductController().SearchProducts(searchTerms);

      topPager.PagedDataSource = pagedDataSource;
      bottomPager.PagedDataSource = pagedDataSource;
      catalogList.PagedDataSource = pagedDataSource;
    }

    #endregion

    #endregion

  }
}
